package org.ns.tomdog.servlet;

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import org.ns.tomdog.server.ServerRequest;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class HttpServletRequest {

    private ServerRequest serverRequest;
    private String contextPath;
    private Map<String, List<String>> parameters = new HashMap<>();
    public HttpServletRequest(ServerRequest serverRequest, String contextPath) {
        this.serverRequest = serverRequest;
        this.contextPath = contextPath;

    }

    public String getContextPath() {
        return this.contextPath;
    }

    public String getContentType() {
        return getHeader("Content-Type");
    }

    public String getHost() {
        return getHeader("Host");
    }

    public long getContentLength() {
        return Long.parseLong(getHeader("Content-Length"));
    }

    public String getHeader(String name) {
        List<String> list = serverRequest.getHeaders().get(name);
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }

    public String gerRequestURL() {
        return serverRequest.getUrl();
    }

    public String getParameter(String name) {
        List<String> list = parameters.get(name);
        if (list == null || list.size() == 0) {
            return null;
        }

        return list.get(0);
    }

    private void parseUrlQueryString(String queryString) {
        if (queryString == null || (queryString = queryString.trim()).isEmpty()) {
            return;
        }
        try {
            String[] split = queryString.split("&");
            for (String param : split) {
                param = URLDecoder.decode(param, "UTF-8");
                int index = param.indexOf("=");
                if (index != -1) {
                    String key = param.substring(0, index).trim();
                    String val = param.substring(index + 1).trim();
                    List<String> list = parameters.get(key);
                    if (list == null) {
                        list = new ArrayList<>();
                        parameters.put(key, list);
                    }
                    list.add(val);
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    private void parseBodyQueryString(ServerRequest serverRequest) {
        if (!getContentType().equals("application/x-www-form-urlencoded")) {
            return;
        }
        if (serverRequest.getMethod().toLowerCase().equals("get")) {
            return;
        }
        try {
            int contentLength = (int) getContentLength();
            InputStream inputStream = serverRequest.getInputStream();
            ByteOutputStream out = new ByteOutputStream(contentLength);
            byte[] bytes = new byte[1024];
            int len = 0;
            int index = 0;
            while (index < contentLength) {
                len = inputStream.read(bytes);
                if (len == -1) {
                    break;
                }
                index += len;
                out.write(bytes, 0 , len);
            }
            byte[] data = out.getBytes();
            String queryString = new String(data, Charset.forName("UTF-8"));
            parseUrlQueryString(queryString);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
